public class VisitorPatter {
    public static void main(String[] args) {
        BuildComputer buildComputer = new BuildComputer();
        Visitor1 visitor1 = new Visitor1();
        buildComputer.build(visitor1);
        int totalPrice = visitor1.totalPrice;
        System.out.println("totalPrice = " + totalPrice);
    }
}

class BuildComputer {
    Cpu cpu = new Cpu();
    Mem mem = new Mem();

    void build(Visitor visitor) {
        cpu.accept(visitor);
        mem.accept(visitor);
    }
}

abstract class ComputerPart {
    abstract void accept(Visitor visitor);
}

class Cpu extends ComputerPart {

    void accept(Visitor visitor) {
        visitor.visitCpu();
    }
}

class Mem extends ComputerPart {

    void accept(Visitor visitor) {
        visitor.visitMem();
    }
}

abstract class Visitor {
    abstract void visitCpu();

    abstract void visitMem();
}

class Visitor1 extends Visitor {
    int totalPrice;

    void visitCpu() {
        totalPrice += 100;
    }

    void visitMem() {
        totalPrice += 200;
    }
}

class Visitor2 extends Visitor {
    int totalPrice;

    void visitCpu() {
        totalPrice += 300;
    }

    void visitMem() {
        totalPrice += 100;
    }
}
